
#ifndef _TTG_THREADS_CRITICALSECTIONS_
#define _TTG_THREADS_CRITICALSECTIONS_
#include "Common/Definitions.h"

namespace ttg_internal
{
	namespace threads
	{
		//Base class for any critical section.
		class CriticalSection
		{
			public:
				//Tries to enter to section.
				//If it's busy, returns false, otherwise locks section and returns true.
				virtual bool tryEnter() = 0;
				//Locks resource.
				virtual void enter() = 0;
				//Unlocks resource.
				virtual void leave() = 0;
				//For descendants.
				virtual ~CriticalSection()
				{ /*nothing*/ }
		};

		//Implementation of fast critical section.
		//So if it's busy, thread will be asking section in while(1) loop.
		class FastCriticalSection :public CriticalSection
		{
			public:
				//Tries to enter to section.
				//If it's busy, returns false, otherwise locks section and returns true.
				virtual bool tryEnter() = 0;
				//Locks resource.
				virtual void enter() = 0;
				//Unlocks resource.
				virtual void leave() = 0;
		};

		//Classic critical section (via mutex).
		class SlowCriticalSection :public CriticalSection
		{
			public:
				//Tries to enter to section.
				//If it's busy, returns false, otherwise locks section and returns true.
				virtual bool tryEnter() = 0;
				//Locks resource.
				virtual void enter() = 0;
				//Unlocks resource.
				virtual void leave() = 0;
		};
	}
}

#include "Win32/Win32CriticalSections.h"

namespace ttg
{
	namespace threads
	{
		typedef ttg_internal::threads::CriticalSection CriticalSection;
	}
}

#endif
